package tw.com.diray.set;

import tw.com.diary.ae.com.DirayBussinessException;
import tw.com.diary.api.domain.UnWriteListDTO;
import tw.com.diary.api.service.FragmentService;
import tw.com.diary.api.service.UnWriteListService;
import tw.com.diary.core.UnWriteListServiceImpl;
import tw.com.diary.db.util.DirDateBean;
import tw.com.diray.CalendarWriteActivity;
import tw.com.diray.R;
import tw.com.diray.RecView;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

/**
 * 當月尚未寫日記清單
 * 
 * @author Andy
 * 
 */

public class UnWriteListFragment extends Fragment implements FragmentService {

	private UnWriteListDTO dto;

	private UnWriteListService serive;

	private UnWriteAdapter adapter;

	private View rootView = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		this.dto = new UnWriteListDTO();
		this.serive = new UnWriteListServiceImpl();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		this.rootView = inflater.inflate(R.layout.fragment_unwrite_list, container, false);

		GridView listview = (GridView) rootView.findViewById(R.id.list);

		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent writeIntent = new Intent(getActivity(), CalendarWriteActivity.class);

				Bundle bundle = new Bundle();
				bundle.putString("yyyymmdd", dto.getUnWriteList().get(position).getYyyymmdd());

				writeIntent.putExtras(bundle);
				getActivity().startActivityForResult(writeIntent, RecView.WRITE_ACTION);

			}
		});

		this.adapter = new UnWriteAdapter();
		listview.setAdapter(this.adapter);

		return rootView;

	}

	private class UnWriteAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return dto.getUnWriteList().size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return dto.getUnWriteList().get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			TextView label = new TextView(getActivity());

			DirDateBean bean = new DirDateBean(dto.getUnWriteList().get(position).getYyyymmdd());
			DirDateBean nowDate = new DirDateBean();

			if (bean.getDate().equals(nowDate.getDate())) {
				label.setBackgroundColor(Color.TRANSPARENT);
			}
			label.setText(bean.getShowDate());
			// TODO Auto-generated method stub
			return label;
		}

	}

	@Override
	public void update() throws DirayBussinessException {
		if (dto.getUnWriteList().size() == 0) {
			rootView.setVisibility(View.GONE);
		} else {
			rootView.setVisibility(View.VISIBLE);
		}
		this.adapter.notifyDataSetChanged();

	}

	@Override
	public void doQuery() throws DirayBussinessException {
		this.serive.queryRecord(dto, getActivity());

	}
}
